import {getCaptcha} from "@/services/wms-dev-tool/login";
import {useEffect, useState} from "react";
import {useRequest} from "@@/plugin-request";

function CaptchaImg(props:any) {
  const [state, setState] = useState(() => {
    return {randCodeImage: '',checkKey:0}
  })
  const {error, run} = useRequest(getCaptcha, {
    manual:true,
    onSuccess:(res,params)=>{
      console.log("getCaptcha---------------->1",res,params);
      setState({
        randCodeImage: res.result,
        checkKey: params[0]
      })
      console.log("getCaptcha---------------->2",state);
      props.onStateChange({
        randCodeImage: res.result,
        checkKey: params[0]
      })
    }
  })
  useEffect(()=>{
    run(new Date().getTime())
  },[])
  return (error?<img src="/checkcode.png" onClick={()=>run(new Date().getTime())}/>:<img src={state.randCodeImage} onClick={()=>run(new Date().getTime())}/>)
}

export default CaptchaImg
